package com.controller.agent;

import com.biz.BdMenuBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.BdMenu;
import com.entity.model.BdUserinfo;
import com.util.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jc on 2019/11/23.
 */
@Controller
@RequestMapping("/agent/security/bdMenu")
public class AgentBdMenuController extends BaseController<BdMenu> {

    @Autowired
    private BdMenuBiz bdMenuBiz;


    @RequestMapping(value = "getMenu")
    @ResponseBody
    public Result getMenu(HttpSession session) {
        BdUserinfo managerInfo = (BdUserinfo) session.getAttribute(SystemConstant.SESSION_AGENT_ADMIN);
        if (managerInfo == null) {
            return ResultGenerator.genFailResult("/agent/loginIndex");
        } else {
            List<BdMenu> list;
            if (managerInfo.getIsSuper() == 1) {
                list = bdMenuBiz.findAll();
                session.setAttribute(SystemConstant.SESSION_RIGHT_LIST, list);
            } else {
                Condition condition = new Condition(BdMenu.class);
                condition.createCriteria().andEqualTo("isSuper", 0);
                list = bdMenuBiz.findByCondition(condition);
                session.setAttribute(SystemConstant.SESSION_RIGHT_LIST, list);
            }
            Map res = new HashMap(2);
            res.put("account", managerInfo);
            res.put("menu", list);
            return ResultGenerator.genSuccessResult(res);
        }
    }

    //获取所有后台菜单按钮
    @RequestMapping(value = "getMenuInfo")
    @ResponseBody
    public Result getMenuInfo(HttpSession session) {
        List<BdMenu> list = bdMenuBiz.findAll();
        session.setAttribute(SystemConstant.SESSION_RIGHT_LIST, list);
        return ResultGenerator.genSuccessResult(list);

    }
}
